<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Menus_model extends MY_Model {

    protected $menu;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "menus";
    }

    function get_menus($filters = array(),$counting = false){
        if (isset($this->CI->cache)) {
            $caching = true;
            $cache_key = 'ci--Menu_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--Menu_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        } else {
            $caching = false;
        }

        $this->db->select('*');
        $this->db->from('menus');
        $param = array();

        if (isset($filters['menu_id'])) {
            $this->db->where('menu_id', $filters['menu_id']);
        }
        if (isset($filters['menu_id_other'])) {
            $this->db->where('menu_id !=', $filters['menu_id_other']);
        }
        $this->db->order_by('menu_id','DESC');
        if (isset($filters['name'])) {
            $this->db->like('name', $filters['name']);
        }
        if (isset($filters['alias'])) {
            $this->db->where('alias', $filters['alias']);
        }
        if (isset($filters['status'])) {
            $this->db->where('status', $filters['status']);
        }

        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $result = $this->db->get();

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
		
        if ($result->num_rows() == 0) {
            return false;
        }
		
        $menu = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('loai-mon/'.$row['alias'], array('rewrite'=>true));
            $menu[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$menu);
        }
        $result->free_result();

        return $menu;
    }

    function get_count($filter = array()){
        return $this->get_menus($filter,true);
    }

    function get_menu($menuId){
        if (!isset($this->menu[$menuId])){
            $menu = $this->get_menus(array('menu_id'=>$menuId));
            $this->menu[$menuId] = $menu;
        } else {
            $menu = $this->menu[$menuId];
        }
        if (empty($menu)) {
            return false;
        }
        return $menu[0];
    }

    function insert_menu($data){
        $_menu_id = $this->save($data);
        return $_menu_id;
    }

    function update_menu($data, $menu_id){
        return $this->update($data, array('menu_id' => $menu_id));
    }

    function delete_menu($menu_id) {
        return $this->delete(array('menu_id'=>$menu_id));
    }

    function change_status_menu($menu_id) {
        $status = $this->db->select('status')->from('menus')->where('menu_id', $menu_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('menu_id' => $menu_id));
    }

    function get_menu_by_alias($menu_alias) {
        $menu = $this->get_menus(array('alias'=>$menu_alias),false);
        if ($menu){
            return $menu[0];
        }
        return false;
    }
	
    function get_menus_options($filters=array()) {
        $menus_options = array();
        $menus = $this->get_menus($filters);
        if (is_array($menus) && count($menus)){
            foreach ($menus as $menu){
                $menus_options[$menu['menu_id']] = $menu['name'];
            }
        }
        return $menus_options;
    }

	function get_menus_withtotal() {
		$sql = "SELECT
                m.menu_id,
                m.name,
                m.alias,
                m.status,
                Count(1) AS total
                from  menus m
                inner join menus_dishs md on m.menu_id = md.menu_id
                inner join dishs  d on d.dish_id = md.dish_id
                where m.status = 1 and d.status = 1 group by m.menu_id";
        $result = $this->db->query($sql);

        return $result->result_array();
    }
}
?>